<?php

namespace System\Types;

class String extends Object
{

    /**
     * @var string
     */
    private $value;

    /**
     * @param string $value
     * @throws \System\Core\Errors\Exceptions
     */
    public function __construct($value = "")
    {
        parent::__construct();
        if (!\is_string($value))
            throw new \System\Core\Errors\Exceptions("O parâmetro informado não é uma string.");

        $this->value = $value;
    }

    /**
     * @param boolean $url_encoded
     * @return String
     */
    public function ClearInvisibleCharacters($url_encoded = true)
    {
        $non_displayables = array();

        // every control character except newline (dec 10)
        // carriage return (dec 13), and horizontal tab (dec 09)

        if (\is_bool($url_encoded) && $url_encoded === true)
        {
            $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
            $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
        }

        $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127

        $str = $this->value;

        do
        {
            $str = \preg_replace($non_displayables, '', $str, -1, $count);
        }
        while ($count);

        /* @var $string String */
        $string = new String();

        $string->value = $str;

        return $string;
    }

    /**
     *
     * @param string $str
     * @param boolean $url_encoded
     * @return String
     */
    public static function RemoveInvisibleCharacters($str, $url_encoded = true)
    {
        $string = new String();

        if (!\is_string($str))
            throw new \System\Core\Errors\Exceptions("O parâmetro informado não é uma string.");

        $string->value = $str;

        return $string->ClearInvisibleCharacters($url_encoded);
    }

    /**
     * @return string
     */
    public function ToString()
    {
        return $this->value;
    }

    /**
     * @param boolean $ucfirst
     * @return string
     */
    public function Camelize($ucfirst = false)
    {
        $str = \str_replace('_', ' ', $this->value);

        $str = \strtolower($str);

        $str = \ucwords($str);

        $str = \str_replace(' ', '', $str);

        if (!$ucfirst)
            $str = \lcfirst($str);

        return $str;
    }

}
